﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CarbonExchange.Bll;
using Acms.DataControls;

public partial class Admin_Questions : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dsSections.Select += new System.EventHandler<EventDrivenDataSourceSelectEventArgs>(dsSections_Select);
        dsSiteQuestions.Select += new EventHandler<EventDrivenDataSourceSelectEventArgs>(dsSiteQuestions_Select);
        dsQuestions.Select += new EventHandler<EventDrivenDataSourceSelectEventArgs>(dsQuestions_Select);

    }

    protected void grid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        Telerik.Web.UI.RadGrid grid = sender as Telerik.Web.UI.RadGrid;
        if(grid != null)
        {
            bool rebind = false;
            int selectedIndex = -1;
            int id = (int)grid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
            SiteQuestionFactory factory = new SiteQuestionFactory();
            if (e.CommandName == "Remove")
            {
                var tmp = factory.GetById(id);
                factory.Delete(tmp);

                rebind = true;
            }
            else if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
            {
                int step = e.CommandName == "MoveUp" ? -1 : 1;
                int sectionId = (int)listSections.SelectedDataKey["Id"];
                var tmp = factory.GetAllBySectionId(sectionId, AcmsContext.SiteId);
                factory.Move(tmp.ToList(), e.Item.ItemIndex, e.Item.ItemIndex + step);

                grid.SelectedIndexes.Clear();
                selectedIndex = e.Item.ItemIndex + step;
                rebind = true;

            }

            if (rebind)
            {
                grid.Rebind();

                Telerik.Web.UI.RadGrid gridUnused = grid.NamingContainer.FindControl("gridUnused") as Telerik.Web.UI.RadGrid;
                if (gridUnused != null)
                {
                    gridUnused.Rebind();
                }
            }

            if (selectedIndex > -1)
            {
                grid.MasterTableView.Items[selectedIndex].Selected = true;
            }
        }
    }

    protected void gridUnused_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Include")
        {
            Telerik.Web.UI.RadGrid gridUnused = sender as Telerik.Web.UI.RadGrid;
            int id = (int)gridUnused.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
            CarbonExchange.Bll.SiteQuestionFactory factory = new CarbonExchange.Bll.SiteQuestionFactory();
            var tmp = new SiteQuestion(AcmsContext.SiteId, id);
            factory.Save(tmp);
            gridUnused.Rebind();

            Telerik.Web.UI.RadGrid grid = gridUnused.NamingContainer.FindControl("grid") as Telerik.Web.UI.RadGrid;
            if (grid != null)
            {
                grid.Rebind();
            }
        }
    }

    void dsQuestions_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        int sectionId = (int)listSections.SelectedDataKey["Id"];
        CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
        e.SetData(factory.GetAllUnusedBySectionId(sectionId, AcmsContext.SiteId));
    }

    void dsSiteQuestions_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        int sectionId = (int)listSections.SelectedDataKey["Id"];
        CarbonExchange.Bll.SiteQuestionFactory siteQuestionFactory = new CarbonExchange.Bll.SiteQuestionFactory();

        e.SetData(siteQuestionFactory.GetAllBySectionId(sectionId, AcmsContext.SiteId));
    }

    void  dsSections_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        CarbonExchange.Bll.SectionFactory factory = new CarbonExchange.Bll.SectionFactory();
        e.SetData(factory.GetAll());
    }

}
